package com.market.service.impl;

import com.market.common.exception.ServiceException;
import com.market.common.vo.ResponseCode;
import com.market.entity.Brand;
import com.market.mapper.BrandMapper;
import com.market.service.BrandService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    BrandMapper brandMapper;

    /**
     * 添加
     *
     * @param brandName
     * @return
     * @throws ServiceException
     */
    @Override
    public int addBrand(String brandName) throws ServiceException {
        int count = 0;
        List<Brand> brands = brandMapper.selectNames();
        for (Brand brand : brands) {
            if (brand.getCommodityBrandName() != brandName) {
                int insert = brandMapper.insert(brandName);
                if (insert > 0) {
                    count = insert;
                    throw new ServiceException(ResponseCode.SUCCESS);
                } else {
                    throw new ServiceException(ResponseCode.ERROR);
                }
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return count;
    }

    /**
     * 修改
     *
     * @param
     * @return
     * @throws ServiceException
     */
    @Override
    public int changeBrand(Integer brandId, String branName) throws ServiceException {
        int i = brandMapper.updateBrand(brandId, branName);
        if (i > 0) {
            throw new ServiceException(ResponseCode.SUCCESS);
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }


    /**
     * 删除
     *
     * @param brandId
     * @return
     * @throws ServiceException
     */
    @Override
    public int deleteBrand(Integer brandId) throws ServiceException {
        int i = brandMapper.deleteBrand(brandId);
        if (i > 0) {
            throw new ServiceException(ResponseCode.SUCCESS);
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 品牌搜索功能
     *
     * @param namesOrId
     * @return
     */
    @Override
    public List<Brand> searchClasses(String namesOrId) throws ServiceException {
        List<Brand> brands = brandMapper.selectNamdesOrId(namesOrId);
        if (brands.size() > 0) {
            return brands;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }
}
